package com.ruoyi.web.controller.youtobemanager;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.setlanguages.io.ChannelInfo;
import com.ruoyi.setlanguages.io.VideoInfo;
import com.ruoyi.setlanguages.service.YouTubeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/youtube/language")
public class YouTubeController {

    @Autowired
    private YouTubeManager youTubeManager;

    @Autowired
    public YouTubeController(YouTubeManager youTubeManager) {
        this.youTubeManager = youTubeManager;
    }

    @GetMapping("/channels")
    public ResponseEntity<List<ChannelInfo>> getMyChannels() {
        try {
            List<ChannelInfo> channels = youTubeManager.getMyChannels();
            return ResponseEntity.ok(channels);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/videos/{channelId}")
    public ResponseEntity<List<VideoInfo>> getChannelVideos(@PathVariable String channelId) {
        try {
            List<VideoInfo> videos = youTubeManager.getChannelVideos(channelId);
            return ResponseEntity.ok(videos);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/video/{videoId}")
    public ResponseEntity<VideoInfo> getVideoDetails(@PathVariable String videoId) {
        try {
            VideoInfo videoInfo = youTubeManager.getVideoDetails(videoId);
            return ResponseEntity.ok(videoInfo);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/video/{videoId}/title")
    public AjaxResult updateVideoTitle(@PathVariable String videoId,
                                       @RequestBody Map<String, String> request) {
        try {
            String newTitle = request.get("title");
            youTubeManager.updateVideoTitle(videoId, newTitle);
            return AjaxResult.success("标题更新成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.success("更新失败  ");
        }
    }

    @PostMapping("/video/{videoId}/multilang")
    public AjaxResult addMultiLanguageTitles(@PathVariable String videoId,
                                             @RequestBody Map<String, String> languageTitles) {
        try {
            youTubeManager.addMultiLanguageTitle(videoId, languageTitles);
            return AjaxResult.success("多语言标题添加成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("添加失败: ");
        }
    }

    @GetMapping("/video/{videoId}/localizations")
    public ResponseEntity<Map<String, String>> getVideoLocalizations(@PathVariable String videoId) {
        try {
            Map<String, String> localizations = youTubeManager.getVideoLocalizations(videoId);
            return ResponseEntity.ok(localizations);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/search")
    public ResponseEntity<List<VideoInfo>> searchVideos(@RequestParam String query,
                                                        @RequestParam(defaultValue = "10") long maxResults) {
        try {
            List<VideoInfo> videos = youTubeManager.searchVideos(query, maxResults);
            return ResponseEntity.ok(videos);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/languages")
    public ResponseEntity<Map<String, String>> getSupportedLanguages() {
        return ResponseEntity.ok(YouTubeManager.getSupportedLanguages());
    }

/*    @GetMapping("/health")
    public AjaxResult healthCheck() {
        return ResponseEntity.ok(Map.of("status", "YouTube服务运行正常"));
    }*/
}